﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Xk
{
    public partial class frmStudentNumGroupByClassSex : Form
    {
        DataSet ds;

        public frmStudentNumGroupByClassSex()
        {
            InitializeComponent();
        }

        private void frmStudentNumGroupByClassSex_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(Properties.Settings.Default.XkConnectionString);
            string sql = "SELECT ClassName,Sex,StudentNum=COUNT(*) FROM Student S,Class C ";
            sql += " WHERE S.ClassNo=C.ClassNo";
            sql += " GROUP BY C.ClassNo,ClassName,Sex";
            sql += " ORDER BY C.ClassNo,Sex";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            ds = new DataSet();
            cn.Open();
            da.Fill(ds, "StudentNumBySex");
            cn.Close();
            dataGridView1.DataSource = ds.Tables["StudentNumBySex"];

            string ClassName = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["ClassName"].ToString() == ClassName)
                {
                    ds.Tables[0].Rows[i]["ClassName"] = "";
                }
                else
                {
                    ClassName = ds.Tables[0].Rows[i]["ClassName"].ToString();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rptStudentNumBySex r = new rptStudentNumBySex();
            r.SetDataSource(ds);
            frmPrint p = new frmPrint();
            p.crystalReportViewer1.ReportSource = r;
            p.ShowDialog();
        }
    }
}
